"""
    自定义的分页组件
"""
from django.utils.safestring import mark_safe


class Pagination(object):

    def __init__(self, request, queryset, page_number=10, step=5):
        # 深拷贝，拿当前请求查询条件
        self.query_dict = request.GET.copy()

        self.page_total_number = queryset.count()  # 总条数
        self.page_number = page_number  # 每页展示的条数
        self.page_total = None  # 总页数
        if self.page_total_number % self.page_number > 0:
            self.page_total = self.page_total_number // self.page_number + 1
        else:
            self.page_total = self.page_total_number // self.page_number

        self.step = step  # 当前页前后要展示的页码个数

        # 当前页码,默认为1，前端有值时取前端内容
        self.page = int(request.GET.get("page", 1))

        self.start_page = 1  # 开始页码,默认为1
        self.end_page = step * 2 + 1  # 结束页码，默认为11

        self.start_number = self.page_number * (self.page - 1)  # 当前页开始的条数值
        self.end_number = self.page_number * self.page  # 当前页结束的条数值

        # 定义一个字符串存储页码
        self.page_list = ''
        # 判断开始及结束密码
        if self.page_total > self.end_page:
            if self.page > step:
                if (self.page + step) * self.page_number >= self.page_total_number:
                    self.start_page = self.page_total - 11
                    self.end_page = self.page_total
                else:
                    self.start_page = self.page - step
                    self.end_page = self.page + step
        else:  # 如果总页数小于等于默认结束页码，则将结束页码调整为总页码
            self.end_page = self.page_total

        # 拼接页码
        # 上一页
        if self.page > 1:
            self.query_dict['page'] = self.page - 1
        else:
            self.query_dict['page'] = self.page
        self.page_list += f'<nav aria-label="Page navigation example"><ul class="pagination pagination-sm"><li class="page-item"><a class="page-link" href="?{self.query_dict.urlencode()}">上一页</a></li>'

        # 循环拼接当前页前后所有页面
        for i in range(self.start_page, self.end_page + 1):
            self.query_dict['page'] = i
            if self.page == i:
                self.page_list += f'<li class="page-item active"><a class="page-link" href="?{self.query_dict.urlencode()}">{i}</a></li>'
            else:
                self.page_list += f'<li class="page-item"><a class="page-link" href="?{self.query_dict.urlencode()}">{i}</a></li>'

        # 下一页
        if self.page < self.page_total:
            self.query_dict['page'] = self.page + 1
        else:
            self.query_dict['page'] = self.page_total
        self.page_list += f'<li class="page-item"><a class="page-link" href="?{self.query_dict.urlencode()}">下一页</a></li>'
        # 尾页
        self.query_dict['page'] = self.page_total
        self.page_list += f'<li class="page-item"><a class="page-link" href="?{self.query_dict.urlencode()}">尾页</a></li>'
        # 拼接跳转到页码
        self.page_list += '<form class="form-inline"><li class="page-item " style="width: 50px" ><input class="page-link" placeholder="页码" type="text" name="page" style="width: 50px; border-radius: 0"></li><li class="page-item "><button class="page-link" type="submit">跳转</button></li></form></ul></nav>'
        # 将最终要返回的字符串设定为安全的给浏览器解析，而不是直接展示。
        self.page_list = mark_safe(self.page_list)
